package markan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class PostSubmit {

	public void send(String sendUrl, String message) {

		URL url = null;
		HttpURLConnection http = null;
		OutputStreamWriter outStream = null;
		PrintWriter writer = null;
		try { 
			url = new URL(sendUrl);
			http = (HttpURLConnection) url.openConnection();
			
			http.setDefaultUseCaches(false);
			http.setDoInput(true); 			// 서버에서 읽기 모드 지정
			http.setDoOutput(true); 		// 서버로 쓰기 모드 지정
			http.setRequestMethod("POST"); 	// 전송 방식은 POST
			http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
			
			// --------------------------
			// 서버로 값 전송
			// --------------------------
			StringBuffer buffer = new StringBuffer();
			buffer.append("message").append("=").append(message);
			
			outStream = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
			writer = new PrintWriter(outStream);
			writer.write(buffer.toString());
			writer.flush();
			
			// 서버에서 전송받기 
            InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "UTF-8");
            BufferedReader reader = new BufferedReader(tmp); 
            StringBuilder builder = new StringBuilder(); 
            String str; 
            while ((str = reader.readLine()) != null) {   
                builder.append(str + "\n");                    
            } 
            String myResult = builder.toString(); 
//            System.out.println(myResult);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(writer!=null) writer.close();
			if(outStream!=null) {try { outStream.close(); } catch (IOException e){ }}
			if(http!=null) http.disconnect();
			
		}
		
		System.out.println("facebook: " + message);
	}
	
//  Http Post로 주고 받기 
   public void HttpPostData() { 
       try { 
             //   URL 설정하고 접속하기 
             URL url = new URL("http://www.ealib.or.kr/eplib2/user/search/search.php?search_id=1");       // URL 설정 
             HttpURLConnection http = (HttpURLConnection) url.openConnection();   // 접속 
             //-------------------------- 
             //   전송 모드 설정 - 기본적인 설정이다 
             //-------------------------- 
             http.setDefaultUseCaches(false);                                            
             http.setDoInput(true);                         // 서버에서 읽기 모드 지정 
             http.setDoOutput(true);                       // 서버로 쓰기 모드 지정  
             http.setRequestMethod("POST");         // 전송 방식은 POST 
                    
             http.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
                     //   서버로 값 전송 
             StringBuffer buffer = new StringBuffer(); 
             buffer.append("key").append("=").append("value").append("&"); 
             OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "EUC-KR"); 
             PrintWriter writer = new PrintWriter(outStream); 
             writer.write(buffer.toString()); 
             writer.flush(); 
            
             // 서버에서 전송받기 
                    
             InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "EUC-KR");  
             BufferedReader reader = new BufferedReader(tmp); 
             StringBuilder builder = new StringBuilder(); 
             String str; 
             while ((str = reader.readLine()) != null) {   
                 builder.append(str + "\n");                    
             } 
             String myResult = builder.toString();                     
       } catch (MalformedURLException e) { 
               // 
       } catch (IOException e) { 
               //  
       }  
   }
}
